/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include <Gluck.h>
#include "Factory.h"
#include "Node.h"

namespace liba
{
namespace lib3d
{
namespace node
{

Factory::FactoryMap * Factory::factory_map = 0;

Node * Factory::create( Node * scene_root, const Atom & object_type, const Atom & name )
{
	FactoryMap::const_iterator it = factory_map->find( object_type );
	if ( it == factory_map->end() )
		return 0;
//		throw Gluck(__FUNCTION__, String() << object_type.get_str() << "not found in factory_map" );
	MakeFun make = it->second;
	return make( scene_root, name );
}

void Factory::add( const Atom & type_name, MakeFun make_fun )
{
	if( !factory_map )
		factory_map = new FactoryMap;
	(*factory_map)[ type_name ] = make_fun;
}

void Factory::remove( const Atom & type_name )
{
	FactoryMap::iterator it = factory_map->find( type_name );
	if ( it != factory_map->end() )
	{
		factory_map->erase( it );
	}
	if( factory_map->empty() )
	{
		delete factory_map; factory_map = 0;
	}
}


} // namespace node
} // namespace lib3d
} // namespace liba


